<?php
require_once APP . '/application/admincontrollers/AdminAction.php';
/**
 * 视频播放
 * @package admin
 */
class ImageController extends AdminAction
{
    
	/**
	 * Enter description here...
	 *
	 * @var imageModel
	 */
	private $image;
    public function init()
    {
    	require_once(APP . '/application/models/ImagesModel.php');
    	
        $this->image = new ImagesModel($this->db);
    }


    /**
     * 加载框架页
     */
    public function addAction()
    {
        //加载默认视图
    }
    
    public function doaddAction()
    {
    	global $cfg;
    	$params = $this->getAllParams();
    	
    	/**************图片处理*********************/
    	$image = "";
    	if (!empty($_FILES) && !empty($_FILES['image']) && !empty($_FILES['image']['name'])){
    		$file_ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
    		$todir = APP . '/upload';
    		if (!file_exists($todir . '/image'))
    			mkdir($todir . '/image');
    		if (!file_exists($todir . '/image/' . date("Ymd")))
    			mkdir($todir . '/image/' . date("Ymd"));
    		
    		$file_ext = strtolower($file_ext);
    		if ($file_ext == 'jpg' || $file_ext == 'jpeg'){
    			$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
    		}elseif ($file_ext == 'png')
    			$im = imagecreatefrompng($_FILES['image']['tmp_name']);
    		elseif ($file_ext == 'gif')
    			$im = imagecreatefromgif($_FILES['image']['tmp_name']);
    		
    		if ($im == false){
    			$this->view->msg = '图片格式不支持';
		    	$this->view->url = $this->url($this->view->baseUrl, "image", "add", "", "admin");
		    	$this->showmessage();
		    	return;
    		}
    		
    		$width = imagesx($im);
		    $height = imagesy($im);
		    
		    // 小图等比缩放
		    $minim = imagecreatetruecolor($cfg['image']['width'], $cfg['image']['height']);
			imagecopyresampled($minim, $im, 0,0, 0,0, $cfg['image']['width'], $cfg['image']['height'], $width, $height);
			
			// 大图，按照宽度等比缩放
			$cfg['image']['maxheight'] = $height / ($width / $cfg['image']['maxwidth']);
			
		    $maxim = imagecreatetruecolor($cfg['image']['maxwidth'], $cfg['image']['maxheight']);
			imagecopyresampled($maxim, $im, 0,0, 0,0, $cfg['image']['maxwidth'], $cfg['image']['maxheight'], $width, $height);
		    
    		$image = '/upload/image/' . date("Ymd") . '/' .date("YmdHis") . '_' . uniqid("h1") . '.jpg';
    		$image2 = '/upload/image/' . date("Ymd") . '/' .date("YmdHis") . '_' . uniqid("h2") . '.jpg';
    		
    		imagejpeg($minim, APP . $image, 100);
    		imagejpeg($maxim, APP . $image2, 100);
    		
    		imagedestroy($im);
    		imagedestroy($minim);
    		unlink($_FILES['image']['tmp_name']);
    	}
    	
    	if (!empty($image)){
	    	$params['image'] = $image;
	    	$params['image2'] = $image2;
    	}
    	
    	$id = $this->image->add($params);
    	
    	   	
    	$this->view->msg = '添加成功';
    	$this->view->url = $this->url($this->view->baseUrl, "image", "add", "", "admin");
    	$this->showmessage();
    }
    
    public function listAction()
    {
    	$nowp = (int) $this->getParam("nowp", 1);
    	$per_page = 20;
    	
    	$result = $this->image->fetchAll($nowp, $per_page);
    	
    	$this->view->nowp = $nowp;
        $this->view->list = $result['list'];
        $this->view->page = $result['page'];
    }
    
    
    public function delAction()
    {
    	$mid = (int) $this->getParam("id");
    	$nowp = (int) $this->getParam("nowp");
    	
    	$content = $this->image->show($mid);
    	$this->image->del($mid);
    	
    	header("location:" . $this->url($this->view->baseUrl, "image", "list", "nowp=" . $nowp, "admin"));
    }
    
    public function editAction()
    {
    	$id = (int) $this->getParam("id");
    	$nowp = (int) $this->getParam("nowp");
    	
        $this->view->nowp = $nowp;
        $this->view->content = $this->image->show($id);
    }
    
    public function doeditAction()
    {
    	global $cfg;
    	$id = (int)$this->getParam("id");
    	$nowp = (int)$this->getParam("nowp");
    	$params = $this->getAllParams();
    	
    	/**************图片处理*********************/
    	$image = "";
    	if (!empty($_FILES) && !empty($_FILES['image']) && !empty($_FILES['image']['name'])){
    		$file_ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
    		$todir = APP . '/upload';
    		if (!file_exists($todir . '/image'))
    			mkdir($todir . '/image');
    		if (!file_exists($todir . '/image/' . date("Ymd")))
    			mkdir($todir . '/image/' . date("Ymd"));
    		
    		$file_ext = strtolower($file_ext);
    		if ($file_ext == 'jpg' || $file_ext == 'jpeg'){
    			$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
    		}elseif ($file_ext == 'png')
    			$im = imagecreatefrompng($_FILES['image']['tmp_name']);
    		elseif ($file_ext == 'gif')
    			$im = imagecreatefromgif($_FILES['image']['tmp_name']);
    		
    		if ($im == false){
    			$this->view->msg = '图片格式不支持';
		    	$this->view->url = $this->url($this->view->baseUrl, "image", "add", "", "admin");
		    	$this->showmessage();
		    	return;
    		}
    		
    		$width = imagesx($im);
		    $height = imagesy($im);
		    
		    // 小图等比缩放
		    $minim = imagecreatetruecolor($cfg['image']['width'], $cfg['image']['height']);
			imagecopyresampled($minim, $im, 0,0, 0,0, $cfg['image']['width'], $cfg['image']['height'], $width, $height);
			
			// 大图，按照宽度等比缩放
			$cfg['image']['maxheight'] = $height / ($width / $cfg['image']['maxwidth']);
			
		    $maxim = imagecreatetruecolor($cfg['image']['maxwidth'], $cfg['image']['maxheight']);
			imagecopyresampled($maxim, $im, 0,0, 0,0, $cfg['image']['maxwidth'], $cfg['image']['maxheight'], $width, $height);
		    
    		$image = '/upload/image/' . date("Ymd") . '/' .date("YmdHis") . '_' . uniqid("h1") . '.jpg';
    		$image2 = '/upload/image/' . date("Ymd") . '/' .date("YmdHis") . '_' . uniqid("h2") . '.jpg';
    		
    		imagejpeg($minim, APP . $image);
    		imagejpeg($maxim, APP . $image2);
    		
    		imagedestroy($im);
    		imagedestroy($minim);
    		unlink($_FILES['image']['tmp_name']);
    	}
    	
    	
    	if (!empty($image)){
	    	$params['image'] = $image;
	    	$params['image2'] = $image2;
    	}else 
    		unset($params['image']);
    	
    	$this->image->change($id, $params);
    	$content = $this->image->show($id);
    	    	
    	$this->view->msg = '编辑成功';
    	$this->view->url = $this->url($this->view->baseUrl, "image", "list", "nowp=" . $nowp, "admin");
    	$this->showmessage();
    }
}
